package com.example.speedometer;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	SensorManager sensorManager;
	TextView myTextView;
	float appliedAcceleration = 0;
	float currentAcceleration = 0;
	float velocity = 0;
	Date lastUpdate;
	Handler handler = new Handler();
	private void updateVelocity() {
		
		
		// Calculate how long this acceleration has been applied.
		Date timeNow = new Date(System.currentTimeMillis());
		long timeDelta = timeNow.getTime() - lastUpdate.getTime();
		lastUpdate.setTime(timeNow.getTime());
		
		
		
		// Calculate the change in velocity at the
		// current acceleration since the last update.
		float deltaVelocity = appliedAcceleration * (timeDelta / 1000);
		appliedAcceleration = currentAcceleration;
		
		// Add the velocity change to the current velocity.
		velocity += deltaVelocity;
		
		
		// Convert from meters per second to miles per hour.
		double mph = (Math.round(velocity / 1.6 * 3.6));
		myTextView.setText(String.valueOf(mph) + "mph");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myTextView = (TextView) findViewById(R.id.myTextView);
		lastUpdate = new Date(System.currentTimeMillis());
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		sensorManager.registerListener(
				new SensorListener() {

					public void onSensorChanged(int sensor, float[] values) {
						// TODO Auto-generated method stub
						double calibration = Double.NaN;

						double x = values[SensorManager.DATA_X];
						double y = values[SensorManager.DATA_Y];
						double z = values[SensorManager.DATA_Z];
						double a = -1
								* Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)
										+ Math.pow(z, 2));
						if (calibration == Double.NaN)
							calibration = a;
						else {
							updateVelocity();
							currentAcceleration = (float) a;

						}
					}

					public void onAccuracyChanged(int sensor, int accuracy) {
						// TODO Auto-generated method stub
 
					}
				}, SensorManager.SENSOR_ACCELEROMETER,
				SensorManager.SENSOR_DELAY_FASTEST);
		
		Timer updateTimer = new Timer("velocityUpdate");
		updateTimer.scheduleAtFixedRate(new TimerTask() {
		public void run() {
		updateGUI();
		}

		private void updateGUI() {
			// TODO Auto-generated method stub
			final double mph = (Math.round(100*velocity / 1.6*3.6)) / 100;
			
			//Update the GUI
			handler.post(new Runnable() {
			public void run() {
			myTextView.setText(String.valueOf(mph) + "mph");
			}
			});
			
		}
		}, 0, 1000);

	}



}



